<?php
namespace App\Traits;

use App\Traits\PageDefaultInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\Paginator;

trait PageTrait
{
    public static function JsonPage(Builder $builder, $pageSize = PageDefaultInterface::PAGE_SIZE, $pageName = PageDefaultInterface::PAGE_NAME)
    {
        $page =  Paginator::resolveCurrentPage($pageName);
        $offset = $page == 1 ? 0 : ($page-1) * $pageSize;
        return [
            'count'         => $builder->count(),
            'list'          => $builder->offset($offset)->limit($pageSize)->get()->toArray(),
            'current_page'  => $page, 
            'page_size'     => $pageSize
        ];
    }
}